package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SrcTxnExcpCfgMapper;
import cc.rengu.oltp.service.common.entity.SrcTxnExcpCfg;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 交易配置表操作方法实现类
 * Created by 王成 on 2017/11/22.
 */
public class SrcTxnExcpCfgMapperImpl implements SrcTxnExcpCfgMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public SrcTxnExcpCfgMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public SrcTxnExcpCfg loadSrcTxnExcpCfgByPrimaryKey(String dbPoolCacheNodeName, String instId, String msgSrcId, String txnNum, String bizType) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCacheNodeName, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSrcTxnExcpCfgByPrimaryKey");
        SrcTxnExcpCfg srcTxnExcpCfg = new SrcTxnExcpCfg();
        srcTxnExcpCfg.setInstId(instId);
        srcTxnExcpCfg.setMsgSrcId(msgSrcId);
        srcTxnExcpCfg.setTxnNum(txnNum);
        srcTxnExcpCfg.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnExcpCfg, srcTxnExcpCfg);
        if (0 != iReturnCode) {
            rglog.warn("查询渠道交易异常配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, msgSrcId, txnNum, bizType, iReturnCode);
            return null;
        }
        return srcTxnExcpCfg;
    }

    @Override
    public SrcTxnExcpCfg selectSrcTxnExcpCfgByPrimaryKey(String instId, String msgSrcId, String txnNum, String bizType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectSrcTxnExcpCfgByPrimaryKey");
        SrcTxnExcpCfg srcTxnExcpCfg = new SrcTxnExcpCfg();
        srcTxnExcpCfg.setInstId(instId);
        srcTxnExcpCfg.setMsgSrcId(msgSrcId);
        srcTxnExcpCfg.setTxnNum(txnNum);
        srcTxnExcpCfg.setBizType(bizType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnExcpCfg, srcTxnExcpCfg);
        if (0 != iReturnCode) {
            rglog.warn("查询渠道交易异常配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>", instId, msgSrcId, txnNum, bizType, iReturnCode);
            return null;
        }
        return srcTxnExcpCfg;
    }

    @Override
    public int updateSrcTxnExcpCfgByPrimaryKey(SrcTxnExcpCfg srcTxnExcpCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateSrcTxnExcpCfgByPrimaryKey");
        srcTxnExcpCfg.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnExcpCfg, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新渠道交易异常配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>",
                    srcTxnExcpCfg.getInstId(), srcTxnExcpCfg.getMsgSrcId(), srcTxnExcpCfg.getTxnNum(), srcTxnExcpCfg.getBizType(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int insertSrcTxnExcpCfg(SrcTxnExcpCfg srcTxnExcpCfg) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertSrcTxnExcpCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, srcTxnExcpCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记渠道交易异常配置信息失败,instId:<{}>,msgSrcId:<{}>,txnNum:<{}>,bizType:<{}>,iReturnCode:<{}>",
                    srcTxnExcpCfg.getInstId(), srcTxnExcpCfg.getMsgSrcId(), srcTxnExcpCfg.getTxnNum(), srcTxnExcpCfg.getBizType(), iReturnCode);
        }
        return iReturnCode;
    }
}
